package yxk.wtc.wtom.business;

import com.alibaba.fastjson.JSON;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.wtc.wtbs.common.constants.WTCBillConstans;
import kd.wtc.wtbs.common.constants.WTCCommonConstants;
import kd.wtc.wtbs.common.constants.relate.WTCBaseConstants;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class OtChangeBillAbandon extends AbstractTask {
    private static final Log LOG = LogFactory.getLog(OtChangeBillAbandon.class);


    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        LOG.info("OtChangeBillAbandon start");
        HRBaseServiceHelper serviceHelper = new HRBaseServiceHelper("wtom_otbillchange");
        QFilter qFilterChange = new QFilter("ischange", QCP.equals, Boolean.TRUE);
        QFilter auditPass = new QFilter(WTCBaseConstants.BILLSTATUS, QCP.equals, WTCCommonConstants.BILLSTATUS_AUDITPASS);
        DynamicObject[] changeBillArr = serviceHelper.loadDynamicObjectArray(new QFilter[]{qFilterChange, auditPass});
        LOG.info("OtChangeBillAbandon changeSize = {}",changeBillArr.length);
        if (changeBillArr.length == 0) {
            LOG.info("OtChangeBillAbandon end");
            return;
        }
        for (DynamicObject changeBill : changeBillArr) {
            Map<String, String> status = new HashMap<>(WTCBaseConstants.INITCAPACITY_HASHMAP);
            status.put(WTCBaseConstants.BILLSTATUS, changeBill.getString(WTCBaseConstants.BILLSTATUS));
            status.put(WTCBillConstans.FIELD_AUDITSTATUS, changeBill.getString(WTCBillConstans.FIELD_AUDITSTATUS));
            Date date = changeBill.getDate(WTCBaseConstants.MODIFYTIME);
            if (date != null) {
                status.put(WTCBaseConstants.MODIFYTIME, date.getTime() + "");
            }
            DynamicObject modifier = changeBill.getDynamicObject(WTCBaseConstants.MODIFIER);
            if (modifier != null) {
                status.put(WTCBaseConstants.MODIFIER, modifier.get(WTCBaseConstants.ID)+"");
            }
            changeBill.set("inputdevicetype", JSON.toJSONString(status));
            changeBill.set(WTCBaseConstants.BILLSTATUS, WTCCommonConstants.AUDITSTATUS_DISCARD);
            changeBill.set(WTCBillConstans.FIELD_AUDITSTATUS, WTCCommonConstants.AUDITSTATUS_DISCARD);
        }
        serviceHelper.save(changeBillArr);
        LOG.info("OtChangeBillAbandon end");
    }
}
